<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <comment-item  @click-reply="$emit('click-reply', $event)" :comment="item" v-for="item in list" :key="item.com_id + ''"></comment-item>
    </van-list>
  </div>
</template>

<script>
import { commentList } from '@/api/article'
import CommentItem from '@/components/comment-item'

export default {
  props: {
    type: {
      default: 'a'
    },
    source: {
      type: [String, Number, Object],
      required: true
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      // list: [],
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },

  created () {
    this.onLoad()
  },

  components: {
    CommentItem
  },

  methods: {
    async onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      try {
        const { data: res } = await commentList({
          type: this.type,
          source: this.source,
          offset: this.offset,
          limit: this.limit
        })
        if (res.data.results.length === 0) {
          this.finished = true
        } else {
          this.offset = res.data.last_id
        }
        this.list.push(...res.data.results)
        this.$emit('updata-total', res.data.total_count)
        console.log(res)
      } catch (err) {
        this.$toast('获取失败')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped lang='less'>

</style>
